package vip.ifmm.lehouse.common.util;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;

/**
 * RocketMq工具类
 *
 * @author Red
 * email: 1318944013@qq.com
 * date: 2019/6/17 22:48
 */
public class RocketMqUtil {

    // 默认broker地址
    private static final String NAMESER_ADDR = "47.101.165.116:9876";
    // 默认消费者组
    private static final String DEFAULT_CONSUMER_GROUP = "default_consumer_group";

    // 获得消费者，可指定Broker地址
    public static DefaultMQPushConsumer getConsumer(String topic, String tags, String nameserAddr) {
        // 创建消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(DEFAULT_CONSUMER_GROUP);
        // 设置地址
        consumer.setNamesrvAddr(nameserAddr);
        // 订阅主题，指定接受的标签 (*接受所有)
        try {
            consumer.subscribe(topic, tags);
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        return consumer;
    }

    // 获得消费者, 使用默认的broker
    public static DefaultMQPushConsumer getConsumer(String topic, String tags) {
        return getConsumer(topic, tags, NAMESER_ADDR);
    }

}
